package com.cg.design.graphicdesignpattern.composite;

import java.util.ArrayList;

/**
 * 表示文件夹的类，是Entry类的子类
 *
 * @program: LeetCode->Directory
 * @description: 表示文件夹的类，是Entry类的子类
 * @author: cg
 * @create: 2022-03-25 18:38
 **/
public class Directory extends Entry {

    /**
     * 文件夹名字
     */
    private final String name;
    /**
     * 用于保存文件夹中的目录条目
     */
    private final ArrayList<Entry> directory = new ArrayList();

    public Directory(String name) {
        this.name = name;
    }

    @Override
    public Entry add(Entry entry) throws FileTreatmentException {
        directory.add(entry);
        entry.parent = this;
        return this;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public int getSize() {
        int size = 0;
        for (Entry entry : directory) {
            size += entry.getSize();
        }
        return size;
    }

    @Override
    public void printList(String prefix) {
        System.out.println(prefix + "/" + this);
        for (Entry entry : directory) {
            entry.printList(prefix + "/" + name);
        }
    }
}
